package com.lianzai.camera.service.impl;

import com.lianzai.camera.dao.CameraBlackAndWhiteDao;
import com.lianzai.camera.model.CameraBlackAndWhiteDO;
import com.lianzai.camera.service.CameraBlackAndWhiteService;
import com.lianzai.common.executor.LocalSvrSyncExecutor;
import com.lianzai.mobile.dao.CommunityInfoDao;
import com.lianzai.mobile.domain.CommunityInfoDO;
import com.lianzai.util.TimeUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class CameraBlackAndWhiteServiceImpl implements CameraBlackAndWhiteService {

	@Autowired
	private CameraBlackAndWhiteDao cameraBlackAndWhiteDao;

	@Autowired
	private CommunityInfoDao communityInfoDao;

	@Override
	public List<CameraBlackAndWhiteDO> list(Map<String, Object> map){
		return cameraBlackAndWhiteDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return cameraBlackAndWhiteDao.count(map);
	}

	@Override
	public int save(Map<String, String> map){
		Integer communityId = Integer.parseInt(String.valueOf(map.get("communityId")));
		CommunityInfoDO communityInfoDO = communityInfoDao.get(communityId);
		if (communityInfoDO != null)
		{
			//向本地系统同步LocalSvrSyncExecutor
			CameraBlackAndWhiteDO cameraBlackAndWhiteDO = new CameraBlackAndWhiteDO();
			cameraBlackAndWhiteDO.setCarNum(String.valueOf(map.get("carNum")));
			cameraBlackAndWhiteDO.setcName(communityInfoDO.getCName());
			cameraBlackAndWhiteDO.setCommunityId(Long.parseLong(String.valueOf(map.get("communityId"))));
			cameraBlackAndWhiteDO.setType(Integer.parseInt(String.valueOf(map.get("type"))));
			cameraBlackAndWhiteDO.setEndTime(TimeUtil.transStrToDate(String.valueOf(map.get("endTime"))));
			cameraBlackAndWhiteDO.setStartTime(TimeUtil.transStrToDate(String.valueOf(map.get("startTime"))));
			LocalSvrSyncExecutor.addBlackListCar(cameraBlackAndWhiteDO,communityInfoDO);
		}
		return cameraBlackAndWhiteDao.save(map);
	}

	@Override
	public Map<String, String> queryExistBlackList(Map<String, String> map){
		return cameraBlackAndWhiteDao.queryExistBlackList(map);
	}

	@Override
	public int update2(Map<String, String> map){
		int ret = cameraBlackAndWhiteDao.update2(map);
		Integer communityId = Integer.parseInt(String.valueOf(map.get("communityId")));
		CommunityInfoDO communityInfoDO = communityInfoDao.get(communityId);
		if(ret == 1){
			//向本地系统同步LocalSvrSyncExecutor
			CameraBlackAndWhiteDO cameraBlackAndWhiteDO = new CameraBlackAndWhiteDO();
			cameraBlackAndWhiteDO.setCarNum(String.valueOf(map.get("carNum")));
			cameraBlackAndWhiteDO.setcName(communityInfoDO.getCName());
			cameraBlackAndWhiteDO.setCommunityId(Long.parseLong(String.valueOf(map.get("communityId"))));
			cameraBlackAndWhiteDO.setType(Integer.parseInt(String.valueOf(map.get("type"))));
			cameraBlackAndWhiteDO.setEndTime(TimeUtil.transStrToDate(String.valueOf(map.get("endTime"))));
			cameraBlackAndWhiteDO.setStartTime(TimeUtil.transStrToDate(String.valueOf(map.get("startTime"))));
			if (communityInfoDO != null)
			{
				LocalSvrSyncExecutor.addBlackListCar(cameraBlackAndWhiteDO,communityInfoDO);//修改也调新增接口
			}

		}
		return ret;
	}

	@Override
	public int update(Map<String, String> map){
		return cameraBlackAndWhiteDao.update(map);
	}

	@Override
	public int remove(String carNum,Long communityId,Integer type){
		Map map =  new HashedMap();
		map.put("carNum",carNum);
		map.put("communityId",communityId);
		int ret = cameraBlackAndWhiteDao.remove(map);
		if(ret == 1){
			CameraBlackAndWhiteDO cameraBlackAndWhiteDO = new CameraBlackAndWhiteDO();
			cameraBlackAndWhiteDO.setCarNum(carNum);
			cameraBlackAndWhiteDO.setCommunityId(communityId);
			cameraBlackAndWhiteDO.setType(type);
			CommunityInfoDO communityInfoDO = communityInfoDao.get(Integer.parseInt(communityId+""));
			if (communityInfoDO != null)
			{
				LocalSvrSyncExecutor.delBlackListCar(cameraBlackAndWhiteDO,communityInfoDO);
			}
		}
		return ret;
	}

	@Override
	public List<Map> listByCond(Map map){
		return cameraBlackAndWhiteDao.listByCond(map);
	}

	@Override
	public List<Map> allList(){
		return cameraBlackAndWhiteDao.allList();
	}
}
